use crate::util::run_command;
use std::path::{Path, PathBuf};

const API_FILE: &str = "src/api.ts";

// 生成api.ts
pub fn run_gen_api(
    config_file_path: &Path,
    project_dir: &Path,
    project_api_json: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let project_api_json = PathBuf::from(project_api_json)
        .to_string_lossy()
        .to_string();
    let config_file_path = PathBuf::from(config_file_path)
        .to_string_lossy()
        .to_string();
    let api_file_path = project_dir.join(API_FILE).to_string_lossy().to_string();
    println!(
        "运行命令:gen_api -i {} -m {} -n {}",
        project_api_json, config_file_path, api_file_path
    );
    run_command(
        Path::new("./"),
        "gen_api",
        &[
            "-i",
            &project_api_json,
            "-m",
            &config_file_path,
            "-n",
            &api_file_path,
        ],
    )?;
    println!("生成api.ts文件:{}", api_file_path);
    Ok(())
}
